package com.luxk.dbt;

import com.luxk.dbt.MainContext.TestType;

public class ExecBlockCase extends ExecBlock {
	
	private int verMin1 = 0;
	private int verMin2 = 0;
	private int verMin3 = 0;
	
	private int verMax1 = Integer.MAX_VALUE;
	private int verMax2 = Integer.MAX_VALUE;
	private int verMax3 = Integer.MAX_VALUE;
	
	public ExecBlockCase(String name, ExecIF parent,
			MainContext mCtx) {
		super(name, parent, mCtx);
		this.name = name;
		this.parent = parent;
		this.mCtx = mCtx;
		
		this.result = new Result(this, name);
	}
	
	@Override
	public void addChild(ExecIF child) throws DBTException {
		this.children.add(child);
		this.result.addChild(child.getResult());
		
		if (!(child instanceof ExecBlockPrework ||
				child instanceof ExecBlockPerformanceTest ||
					child instanceof ExecBlockPostwork))
			throw new DBTException(
				child.getClass().getName() + 
				" is unsupported class to add child", this.line);
		
		if(this.mCtx.getTestType() == TestType.FUNCTIONAL_TEST &&
				child instanceof ExecBlockPerformanceTest)
			throw new DBTException(
					"wrong case for functional test", this.line);

		if(this.mCtx.getTestType() == TestType.PERFORMANCE_TEST &&
				child instanceof ExecBlockFunctionalTest)
			throw new DBTException(
					"wrong case for performance test", this.line);

	}

	@Override
	public void prepareInternal() throws DBTException {
		String driver = null;
		String url = null;
		String user = null;
		String password = null;
		
		driver = this.mCtx.getProperties("connection.default.driver");
		url = this.mCtx.getProperties("connection.default.url");
		
		user = this.mCtx.getProperties("connection.default.user");
		password =
			this.mCtx.getProperties("connection.default.password");

		this.sCtx = new SessionContext();
		this.sCtx.setConnectionInfo(driver, url, user, password,
				this.mCtx.getProgName());
		
		// connect to db, init runtime, etc...
		try {
			this.sCtx.getConnection();
		} catch(Exception e) {
			throw new DBTException(e);
		}
	}
	
	@Override
	public void cleanupInternal() throws DBTException {}
	
	@Override
	public ExecIF duplicate(ExecIF parent) throws DBTException {
		throw new DBTException(
				this.getClass().getName() + " unsupport duplicate",
				this.line);
	}
	
	@Override
	public SessionContext getSessionContext() throws DBTException {
		return this.sCtx;
	}
	
	public void setVersion(String str) throws DBTException {
		
		if(this.mCtx.isDebug())
			this.mCtx.writeDebug("setVersion " + str);
		
		if (str == null || "".equals(str))
			return;
		
		String tmp[] = str.split("-");
		
		if (tmp == null || tmp.length == 0) {
			setVersionMin(str);
			setVersionMax(str);
		} else if (tmp.length == 1) {
			setVersionMin(tmp[0]);
			setVersionMax(tmp[0]);
		} else if (tmp.length > 1) {
			setVersionMin(tmp[0]);
			setVersionMax(tmp[1]);
		}
		
		if(this.mCtx.isDebug())
			this.mCtx.writeDebug(
				String.format("%d.%d.%d - %d.%d.%d",
						this.verMin1, this.verMin2, this.verMin3,
						this.verMax1, this.verMax2, this.verMax3));
	}
	
	private void setVersionMin(String str) throws DBTException {

		if (str == null || "".equals(str))
			return;
		
		String tmp[] = str.split("[^0-9]");
		
		if (tmp == null || tmp.length == 0)
			return;
		
		if (tmp.length == 1) {
			this.verMin1 = Integer.parseInt(tmp[0]);
			return;
		}
		
		if (tmp.length == 2) {
			this.verMin1 = Integer.parseInt(tmp[0]);
			this.verMin2 = Integer.parseInt(tmp[1]);
			return;
		}
		
		if (tmp.length > 2) {
			this.verMin1 = Integer.parseInt(tmp[0]);
			this.verMin2 = Integer.parseInt(tmp[1]);
			this.verMin3 = Integer.parseInt(tmp[2]);
			return;
		}
	}
	
	private void setVersionMax(String str) throws DBTException {
		
		if (str == null || "".equals(str))
			return;
		
		String tmp[] = str.split("[^0-9]");
		
		if (tmp == null || tmp.length == 0)
			return;
		
		if (tmp.length == 1) {
			this.verMax1 = Integer.parseInt(tmp[0]);
			return;
		}
		
		if (tmp.length == 2) {
			this.verMax1 = Integer.parseInt(tmp[0]);
			this.verMax2 = Integer.parseInt(tmp[1]);
			return;
		}
		
		if (tmp.length > 2) {
			this.verMax1 = Integer.parseInt(tmp[0]);
			this.verMax2 = Integer.parseInt(tmp[1]);
			this.verMax3 = Integer.parseInt(tmp[2]);
			return;
		}		
	}
}
